package com.ckm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckm.entity.Course;
import com.ckm.entity.StudentCourse;
import com.ckm.entity.TeacherCourse;
import com.ckm.exception.BusinessException;
import com.ckm.mapper.*;
import com.ckm.service.TeacherCourseService;
import com.ckm.util.FindObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class TeacherCourseServiceImpl implements TeacherCourseService {

    @Autowired
    private TeacherCourseMapper teacherCourseMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StudentCourseMapper studentCourseMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public void addTeacherCourse(List<TeacherCourse> teacherCourseList) {
        if (teacherCourseList == null || teacherCourseList.isEmpty()) {
            throw new BusinessException(250,"添加数据集为空");
        }
        for (TeacherCourse teacherCourse : teacherCourseList) {
            if (!findTeacherCourse(teacherCourse).isEmpty()) {
                throw new BusinessException(250,"数据重复:"+teacherCourse);
            }
            if (teacherMapper.selectById(teacherCourse.getTchId()) == null){
                throw new BusinessException(250,"无法找到老师："+teacherCourse.getTchId());
            }
            if (courseMapper.selectById(teacherCourse.getCourseId()) == null){
                throw new BusinessException(250,"无法找到课程："+teacherCourse.getCourseId());
            }
            if (teacherCourse.getClassId() != null){
                if (classesMapper.selectById(teacherCourse.getClassId()) == null){
                    throw new BusinessException(250,"无法找到班级："+teacherCourse.getClassId());
                }
            }
            teacherCourseMapper.insert(teacherCourse);
        }
        log.info("添加数据：{}", teacherCourseList);
    }

    @Override
    public void deleteTeacherCourse(Integer id) {
        if (id==null){
            throw new BusinessException(250,"删除参数为空");
        }
        if (teacherCourseMapper.selectById(id)==null){
            throw new BusinessException(250,"无此任课");
        }
        teacherCourseMapper.deleteById(id);
    }

    @Override
    public void updateTeacherCourse(TeacherCourse teacherCourse) {
        if (teacherCourse.getId()==null){
            throw new BusinessException(250,"更新信息缺少id");
        }
        TeacherCourse select = teacherCourseMapper.selectById(teacherCourse.getId());
        if (select==null){
            throw new BusinessException(250,"无此任课");
        }
        if (select.equals(teacherCourse)){
            throw new BusinessException(250,"信息无更改");
        }
        teacherCourseMapper.updateById(teacherCourse);
    }

    @Override
    public PageInfo<TeacherCourse> getTeacherCoursePage(TeacherCourse teacherCourse, int pageNum, int pageSize) {
        teacherCourseMapper.getTeacherCourseInfoPage(teacherCourse);
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(teacherCourseMapper.getTeacherCourseInfoPage(teacherCourse));
    }

    @Override
    public List<TeacherCourse> findTeacherCourse(TeacherCourse teacherCourse) {
        QueryWrapper<TeacherCourse> wrapper = new QueryWrapper<>();
        FindObjectUtil.fullQueryWrapper(teacherCourse,wrapper);
        return teacherCourseMapper.selectList(wrapper);
    }

    @Override
    public void openChooseCourse(List<TeacherCourse> courseNameList) {
        for (TeacherCourse teacherCourse : courseNameList) {
            Course course = courseMapper.selectOne(new QueryWrapper<Course>().eq("course_id", teacherCourse.getCourseId()));
            if (course == null) {
                throw new BusinessException(250,"查找不到该课程");
            }
            String key = "choose"+course.getCourseName();
            redisTemplate.delete(key);
            Long count = studentCourseMapper.selectCount(new QueryWrapper<StudentCourse>().eq("tch_course_id", teacherCourse.getId()));
            redisTemplate.opsForValue().set(key,course.getSize()-count,5, TimeUnit.HOURS);
        }

    }
}
